<?php
namespace api\modules\h5\controllers\favorites;

use backend\widgets\provinces\Provinces;
use common\models\cart\Cart;
use common\models\goods\Goods;
use yii\data\ActiveDataProvider;
use Yii;
use yii\web\NotFoundHttpException;
use api\controllers\OnAuthController;
use common\enums\StatusEnum;
use common\helpers\ResultDataHelper;
use common\models\store\Store;

/**
 * 收藏接口
 *
 * Class InfoController
 * @package api\modules\h5\controllers\favorites
 * @property \yii\db\ActiveRecord $modelClass
 * @author tjj
 */
class InfoController extends OnAuthController
{
    public $modelClass = 'common\models\favorites\Favorites';



    /**
     * 删除收藏
     * @param $id
     * @return bool
     * @throws NotFoundHttpException
     */
    public function actionDelete($id)
    {
        $favorites = $this->modelClass::findOne($id);

        if($favorites)
        {
            if($favorites->member_id != Yii::$app->user->identity->member_id)
                return ResultDataHelper::api(422, '权限有误');

            $favorites->status = StatusEnum::DELETE;
            return $favorites->save();
        }else
            return ResultDataHelper::api(422, '数据不存在');
    }

    /**
     * 添加收藏
     * @return mixed|\yii\db\ActiveRecord
     */
    public function actionCreate()
    {
        $request = Yii::$app->request;

        $check = $this->modelClass::findOne([
            'member_id' => Yii::$app->user->identity->member_id,
            'fav_type' => $request->post('fav_type')??'',
            'fav_id' => $request->post('fav_id')??0,
            'status'=>StatusEnum::ENABLED
        ]);

        if($check)
            return ResultDataHelper::api(422, '已经收藏过');

        return parent::actionCreate(); // TODO: Change the autogenerated stub
    }

    /**
     * 列表
     * @return ActiveDataProvider
     */
    public function actionIndex()
    {
        $request = Yii::$app->request;
        $type = $request->get('type')??'';

        if(!in_array($type,['Store','Goods']))
            return ResultDataHelper::api(422, '类型有误');

        $query = $this->modelClass::find()
            ->where(['member_id'=>Yii::$app->user->identity->member_id])
            ->andWhere(['status'=>StatusEnum::ENABLED])
            ->andWhere(['fav_type'=>$type])
            ->select([])
            ->with(['Store'=>'store','Goods'=>'goods'][$type])
            ->asArray();

        return new ActiveDataProvider([
            'query' => $query,
            'pagination' => [
                'pageSize' => Yii::$app->params['user.pageSize'],
                'validatePage' => false,// 超出分页不返回data
            ],
        ]);
    }

    /**
     * 权限验证
     *
     * @param string $action 当前的方法
     * @param null $model 当前的模型类
     * @param array $params $_GET变量
     * @throws \yii\web\BadRequestHttpException
     */
    public function checkAccess($action, $model = null, $params = [])
    {
        // 方法名称
        if (in_array($action, ['view','update']))
        {
            throw new \yii\web\BadRequestHttpException('权限不足');
        }
    }
}
